<template>
  <div class="home-view">
    <h1>Socket.IO 实时通信示例</h1>
    
    <div class="connection-status" :class="{ connected: isConnected }">
      连接状态: {{ isConnected ? '已连接' : '未连接' }}
      <span v-if="isConnected"> (在线客户端: {{ clientCount }})</span>
      <button 
        @click="connect" 
        v-if="!isConnected"
        class="reconnect-btn"
      >
        重新连接
      </button>
    </div>
    
    <div class="message-container">
      <div class="message-list">
        <!-- 消息列表（仅保留这一处渲染） -->
        <div 
          v-for="(msg, index) in messages" 
          :key="index" 
          class="message" 
          :class="{ 
            incoming: msg.incoming,
            'server-push': msg.type === 'server-push',
            'server-broadcast': msg.type === 'server-broadcast'
          }"
        >
          <span class="sender">{{ msg.incoming ? '服务器' : '你' }}:</span>
          <span class="content">{{ msg.text }}</span>
          <span class="time">{{ formatTime(msg.timestamp) }}</span>
        </div>

        <!-- 空状态提示 -->
        <div v-if="messages.length === 0" class="empty-message">
          暂无消息，连接后可发送消息...
        </div>
      </div>
      
      <div class="message-input">
        <input 
          type="text" 
          v-model="messageText" 
          @keyup.enter="sendMessage"
          placeholder="输入消息..."
          :disabled="!isConnected"
        >
        <button 
          @click="sendMessage"
          :disabled="!isConnected || !messageText.trim()"
        >
          发送
        </button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { useSocket } from '@/services/socketService';

export default defineComponent({
  name: 'HomeView',
  setup() {
    const { 
      isConnected, 
      clientCount, 
      messages, 
      messageText, 
      sendMessage, 
      formatTime,
      connect
    } = useSocket();
    
    return {
      isConnected,
      clientCount,
      messages,
      messageText,
      sendMessage,
      formatTime,
      connect
    };
  }
});
</script>

<style scoped>
.home-view {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

.connection-status {
  padding: 10px;
  margin-bottom: 20px;
  border-radius: 4px;
  background-color: #f8d7da;
  color: #721c24;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.connection-status.connected {
  background-color: #dff0d8;
  color: #3c763d;
}

.reconnect-btn {
  padding: 5px 10px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

.reconnect-btn:hover {
  background-color: #0056b3;
}

.message-container {
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow: hidden;
}

.message-list {
  height: 400px;
  overflow-y: auto;
  padding: 10px;
  background-color: #f9f9f9;
}

.empty-message {
  text-align: center;
  padding: 40px 0;
  color: #999;
  font-style: italic;
}

.message {
  margin-bottom: 10px;
  padding: 8px 12px;
  border-radius: 4px;
  max-width: 70%;
  word-wrap: break-word;
}

.message.incoming {
  background-color: #e3f2fd;
  margin-right: auto;
}

.message:not(.incoming) {
  background-color: #e8f5e9;
  margin-left: auto;
}

/* 服务器推送消息特殊样式 */
.message.server-push {
  border-left: 3px solid #ff9800;
}

.message.server-broadcast {
  border-left: 3px solid #f44336;
}

.sender {
  font-weight: bold;
  margin-right: 8px;
}

.time {
  font-size: 0.8em;
  color: #666;
  margin-left: 8px;
}

.message-input {
  display: flex;
  padding: 10px;
  border-top: 1px solid #ddd;
  background-color: white;
}

.message-input input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-right: 10px;
}

.message-input input:disabled {
  background-color: #f9f9f9;
  cursor: not-allowed;
}

.message-input button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.message-input button:disabled {
  background-color: #a5d6a7;
  cursor: not-allowed;
}

.message-input button:not(:disabled):hover {
  background-color: #359e75;
}
</style>